// Copyright (c) 2008, 
// Marc Speck, http://www.faindu.com
// All Rights Reserved. The following is Source Code and is subject to all restrictions 
// on such code as contained in the license accompanying this product.
package com.faindu.editImage.flip
{
	import com.faindu.editImage.EditImage;
	import com.faindu.editImage.base.BaseTool;
	
	import flash.geom.Matrix;

/**
 * Tool that flips the image horizontally and vertically. By default, 
 * <code>ScaleTool</code> is disabled.
 */	
public class FlipTool extends BaseTool implements IFlipTool
{
	
	private var editImage: EditImage
	
    //==========================================================================
    //
    //  Methods
    //
    //--------------------------------------------------------------------------  		
	/**
	 * @inheritDoc
	 */	
	override public function init( editImage:EditImage, startInteractionHandler:Function ): void
	{
		// initialize properties
		this.editImage = editImage
	}

    //==========================================================================
    //
    //  Flip interaction
    //
    //--------------------------------------------------------------------------  	
	/**
	 * @inheritDoc
	 */
	public function flipHorizontally(): void
	{
		var toolMatrix: Matrix =  editImage.toolMatrix.clone()
		toolMatrix.a = - toolMatrix.a
		toolMatrix.b = - toolMatrix.b
		
		updateMatrix( toolMatrix )
	}

	/**
	 * @inheritDoc
	 */
	public function flipVertically(): void
	{
		var toolMatrix: Matrix =  editImage.toolMatrix.clone()
		toolMatrix.c = - toolMatrix.c
		toolMatrix.d = - toolMatrix.d
		
		updateMatrix( toolMatrix )
	}

	private function updateMatrix( updatedMatrix:Matrix ): void
	{
		editImage.toolMatrix = updatedMatrix
		editImage.imageHolder.transform.matrix = updatedMatrix.clone()
		
		editImage.updateControlsPositions()
		editImage.drawToolControls()
		editImage.invalidateSize()
	}

}
}